package casts.misc
{
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.Loader;
   import flash.display.MovieClip;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.net.URLRequest;
   // greensock
   import com.greensock.TweenMax;

   /**
    * 圖片容器，單純就是讀圖。
    * @author     cjboy | cjboy1984@gmail.com
    * @usage
    * loadImg('img/normal.jpg');
    */
   public class PhotoContainer_1 extends MovieClip
   {
      // 圖片顯示的範圍
      private const FIX_WIDTH:Number = 100;
      private const FIX_HEIGHT:Number = 100;

      // loader
      private var msk:Sprite = new Sprite();
      private var bmpLoader:Loader;
      private var bmp:Bitmap;

      public function PhotoContainer_1()
      {
         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // --------------------- LINE ---------------------

      public function loadImg(img:String):void
      {
         if (!img) return;

         // 先清掉舊資料
         if (bmpLoader)
         {
            bmpLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOErr);
            bmpLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onBmpLoaded);
            bmpLoader.unloadAndStop();
            bmpLoader = null;
         }

         bmpLoader = new Loader();
         bmpLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOErr);
         bmpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBmpLoaded);
         bmpLoader.load(new URLRequest(img));
      }

      // ################### protected ##################

      // #################### private ###################

      private function onAdd(e:Event):void
      {
      }

      private function onRemove(e:Event):void
      {
         if (contains(msk))
         {
            removeChild(msk);
         }

         if (bmp)
         {
            TweenMax.killTweensOf(bmp);
         }

         if (bmpLoader)
         {
            bmpLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOErr);
            bmpLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onBmpLoaded);
            bmpLoader.unloadAndStop();
            bmpLoader = null;
         }
      }

      // --------------------- LINE ---------------------

      private function onBmpLoaded(e:Event):void
      {
         bmpLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOErr);
         bmpLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onBmpLoaded);

         bmp = Bitmap(bmpLoader.content);
         bmp.alpha = 0;
         bmp.smoothing = true;
         MyUtils.scaleToFillArea(bmp, FIX_WIDTH, FIX_HEIGHT, 'proportionalOutside');
         bmp.x = (FIX_WIDTH - bmp.width) / 2;
         bmp.y = (FIX_HEIGHT - bmp.height) / 2;
         bmp.mask = msk;
         addChild(bmp);

         // create mask
         msk.graphics.clear();
         msk.graphics.beginFill(0x0000ff);
         msk.graphics.drawRect(0, 0, FIX_WIDTH, FIX_HEIGHT);
         msk.graphics.endFill();
         addChild(msk);

         TweenMax.to(bmp, 0.5, { alpha:1 } );
      }

      private function onIOErr(e:IOErrorEvent):void
      {
         bmpLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOErr);
         bmpLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onBmpLoaded);
      }

      // --------------------- LINE ---------------------

   }

}